package run;

import processing.core.PApplet;
import processing.core.PVector;

public class RepulseRule implements DistRule {
	
	float dir = 0;
	
	public PVector execRule(LineCell a, LineCell b, Env e, PApplet p) {
		
		// lazy initialization
		if (dir == 0) {
			if (p.random(1) < 0.9)
				dir = -1;
			else dir = 1;
		}
		
		if (a != b) {
			PVector diff = PVector.sub(a.loc, b.loc);
			
			diff.normalize();
			
			PVector rep = new PVector(1, 1, 1);
			
			float dist = a.loc.dist(b.loc);
			
			// a MAX Turn Rate (x radians per step)
			if (dist > 0) {
				rep.div(dist);
				rep.div(dist);
				rep.mult(dir * 2F);
				return rep;
			}
			
			return new PVector(0,0,0);
		}
		return new PVector(0,0,0);
	}
}
